package org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.udp.advanced;

import io.netty.channel.Channel;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyChannelContext;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.payload.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.permeate.udp.server.AbstractHandleUdpReportClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.permeate.udp.socket.NettyUdpClientPermeateServerConnectRealSocket;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Component;

/**
 * 上报客户端通信通道连接成功
 */
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@Component
public class ServerHandleUdpReportClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced extends AbstractHandleUdpReportClientTransferServerPermeateChannelConnectionSuccessfulTypeAdvanced<NettyProxyMsg> {
    /**
     * 处理当前数据
     *
     * @param nettyChannelContext 当前通道
     * @param nettyProxyMsg       通道数据
     */
    @Override
    protected void doHandler(NettyChannelContext nettyChannelContext, NettyProxyMsg nettyProxyMsg) {
        Channel channel = nettyChannelContext.channel();
        // 创建目标地址连接
        byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
        byte[] msgVisitorPort = nettyProxyMsg.getVisitorPort();
        byte[] msgClientTargetIp = nettyProxyMsg.getTargetIp();
        byte[] msgClientTargetPort = nettyProxyMsg.getTargetPort();
        byte[] msgClientId = nettyProxyMsg.getClientId();
        String clientId = new String(msgClientId);
        // 绑定客户端ID
        ChannelAttributeKeyUtils.buildClientId(channel, clientId);
        ChannelAttributeKeyUtils.buildVisitorId(channel, msgVisitorId);
        NettyUdpClientPermeateServerConnectRealSocket.buildNewRealServer(new String(msgVisitorId),
                Integer.parseInt(new String(msgVisitorPort)),
                new String(msgClientTargetIp),
                Integer.parseInt(new String(msgClientTargetPort)),
                channel
        );


    }
}
